<template>
  <div class="app-container">
    <a-card class="general-card" title="新设备申请">
      <a-row>
        <a-col :flex="1">
          <a-form
              :model="queryParams"
              ref="queryRef"
              :inline="true"
              v-show="showSearch"
              :label-col-props="{ span: 6 }"
              :wrapper-col-props="{ span: 18 }"
              label-align="left"
          >
            <a-row :gutter="16">
              <a-col :span="8">
                <a-form-item label="创建时间" field="createTime">
                  <a-range-picker
                      v-model="dateRange"
                      style="width: 100%"
                  />
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </a-col>
        <a-divider style="height: 84px" direction="vertical" />
        <a-col :flex="'86px'" style="text-align: right">
          <a-space direction="vertical" :size="18">
            <a-button type="primary" @click="handleQuery">
              <template #icon>
                <icon-search />
              </template>
              搜索
            </a-button>
            <a-button @click="resetQuery">
              <template #icon>
                <icon-refresh />
              </template>
              重置
            </a-button>
          </a-space>
        </a-col>
      </a-row>
    </a-card>
    <a-row :gutter="10" class="mb8">
      <a-col :span="1.5">
        <a-button
            size="small"
            type="primary"
            @click="handleAdd"
        >
          <template #icon>
            <icon-plus/>
          </template>
          新增
        </a-button>
      </a-col>
      <a-col :span="1.5">
        <a-button
            type="primary"
            size="small"
            status="success"
            :disabled="single"
            @click="handleUpdate"
        >
          <template #icon>
            <icon-edit/>
          </template>
          修改
        </a-button>
      </a-col>
      <a-col :span="1.5">
        <a-button
            type="primary"
            size="small"
            status="danger"
            :disabled="multiple"
            @click="handleDelete"
        >
          <template #icon>
            <icon-delete/>
          </template>
          删除
        </a-button>
      </a-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </a-row>

    <a-table
        v-model:selectedKeys="selectedKeys"
        :row-selection="rowSelection"
         row-key="id"
        :loading="loading"
        :pagination="false"
        :data="customerInfoList"
        :bordered="false"
        @selection-change="handleSelectionChange"
    >
      <template #columns>
        <a-table-column
            fixed="left"
            title="申请地点" align="center" data-index="address" :width="100" />
        <a-table-column data-index="resultStatus" title="申请类型" width="100">
          <template #cell="{record}">
            <dict-tag :options="bis_sb_type" :value="record.equipmentType" />
          </template>
        </a-table-column>
        <a-table-column
            title="设备名称" align="center" data-index="equipmentName" :width="150"/>
        <a-table-column title="设备数量" align="center"  data-index="num" :width="100" />
        <a-table-column title="设备金额" align="center"  data-index="amount" :width="100" />
        <a-table-column title="内容" align="center" data-index="descInfo" :width="100" />
        <a-table-column title="申请人" align="center" data-index="person" :width="100" />
        <a-table-column title="联系电话" align="center"  data-index="phone" :width="100" />
        <a-table-column data-index="resultStatus" title="审批状态" width="100">
          <template #cell="{record}">
            <dict-tag :options="bis_sp_res" :value="record.resultStatus" />
          </template>
        </a-table-column>
        <a-table-column title="审批结果" align="center" data-index="resultRemark" :width="100" />
        <a-table-column
            title="申请时间" align="center" data-index="createTime" :width="175" />
        <a-table-column
            fixed="right"
            title="操作" align="center" width="200">
          <template #cell="{ record }">
            <a-button
                type="text"
                icon="Edit"
                @click="handleUpdate(record)"
            >修改</a-button>
            <a-button
                type="text"
                icon="Delete"
                @click="handleDelete(record)"
            >删除</a-button>
          </template>
        </a-table-column>
      </template>
    </a-table>

    <!--分页-->
    <div style="margin-top: 30px;float: right">
      <a-pagination :total="total" show-total show-jumper show-page-size
                    v-model:current="queryParams.pageNum"
                    v-model:page-size="queryParams.pageSize"
                    @change="onPageChange"
                    @page-size-change="onPageSizeChange"
      />
    </div>

    <!-- 添加或修改客户对话框-->
    <a-drawer :visible="addOrUpdateDrawerVisible" @ok="handleAddOrUpdateOk" @cancel="handleAddOrUpdateCancel" unmountOnClose :width="540">
      <template #title>
        {{title}}
      </template>
      <a-form ref="equipmentApplicationRef" :model="form" :rules="rules" label-width="80px">
        <a-row>
          <a-col :span="20">
            <a-form-item label="申请地点" field="address">
              <a-input v-model="form.address" placeholder="请输入申请地点" />
            </a-form-item>
          </a-col>
          <a-col :span="20">
            <a-form-item label="审批类别" field="equipmentType">
              <a-select v-model="form.equipmentType" placeholder="请选择">
                <a-option
                    v-for="dict in bis_sb_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></a-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="20">
            <a-form-item label="设备名称" field="equipmentName">
              <a-input v-model="form.equipmentName" placeholder="请输入设备名称" />
            </a-form-item>
          </a-col>
          <a-col :span="20">
            <a-form-item label="设备数量" field="num">
              <a-input v-model="form.num" placeholder="请输入设备数量" />
            </a-form-item>
          </a-col>
          <a-col :span="20">
            <a-form-item label="设备金额" field="amount">
              <a-input v-model="form.amount" placeholder="请输入设备金额" />
            </a-form-item>
          </a-col>
          <a-col :span="20">
            <a-form-item label="内容" field="descInfo">
              <a-input v-model="form.descInfo" placeholder="请输入内容" />
            </a-form-item>
          </a-col>
          <a-col :span="20">
            <a-form-item label="申请人" field="person">
              <a-input v-model="form.person" placeholder="请输入申请人" />
            </a-form-item>
          </a-col>
          <a-col :span="20">
            <a-form-item label="联系电话" field="phone">
              <a-input v-model="form.phone" placeholder="请输入联系电话" />
            </a-form-item>
          </a-col>
          <a-col :span="20">
            <a-form-item label="审批状态" field="resultStatus">
              <a-select v-model="form.resultStatus" placeholder="请选择">
                <a-option
                    v-for="dict in bis_sp_res"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></a-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :span="20">
            <a-form-item label="审批结果" field="resultRemark">
              <a-input v-model="form.resultRemark" placeholder="请输入审批结果信息" />
            </a-form-item>
          </a-col>
          <a-col :span="20">
            <a-form-item label="备注信息" field="remark">
              <a-textarea
                  :rows="6"
                  type="textarea"
                  placeholder="请输入内容"
                  v-model="form.remark"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-drawer>
  </div>
</template>

<script setup name="CustomerList">
import {getCurrentInstance, reactive, ref} from "vue";
import {getEquipmentApplication,getEquipmentApplicationInfoList,addEquipmentApplicationInfo,updateEquipmentApplicationInfo,delEquipmentApplicationInfo} from "@/api/accept/equipmentApplication";

const selectedKeys = ref([]);
const rowSelection = reactive({
  type: 'checkbox',
  showCheckedAll: true,
});
const { proxy } = getCurrentInstance();
const { bis_sp_res } = proxy.useDict("bis_sp_res");
const { bis_sb_type } = proxy.useDict("bis_sb_type");

const customerInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const addOrUpdateDrawerVisible = ref(false)

// 添加或更新对话框
const handleAddOrUpdateOk = () => {
  submitForm()
}

const handleAddOrUpdateCancel = () => {
  addOrUpdateDrawerVisible.value = false;
}

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {
    address: [{ required: true, message: "申请地点不能为空", trigger: "blur"}],
    equipmentType: [{ required: true, message: "申请类型不能为空", trigger: "blur"}],
    equipmentName: [{ required: true, message: "设备名称不能为空", trigger: "blur"}],
    num: [{ required: true, message: "设备数量不能为空", trigger: "blur"}],
    amount: [{ required: true, message: "设备金额不能为空", trigger: "blur"}],
    person: [{ required: true, message: "申请人不能为空", trigger: "blur"}],
    phone: [{ required: true, message: "联系电话不能为空", trigger: "blur"}],
  },
});

const { queryParams, form,rules } = toRefs(data);

const onPageChange = (current) => {
  queryParams.value.pageNum = current
  getList()
};
const onPageSizeChange = (pageSize) => {
  queryParams.value.pageSize = pageSize
  getList()
};

/** 查询新设备申请列表 */
function getList() {
  queryParams.value.startTime = dateRange.value[0];
  queryParams.value.endTime = dateRange.value[1];
  loading.value = true;
  getEquipmentApplicationInfoList(queryParams.value).then(response => {
    customerInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  queryParams.value = {}
  form.value = {
    name: undefined,
    phone: undefined,
    remark: undefined
  };
  proxy.resetForm("equipmentApplicationRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  addOrUpdateDrawerVisible.value = true;
  title.value = "添加设备申请";
}
/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  const equipmentApplicationId = row.id || ids.value;
  getEquipmentApplication(equipmentApplicationId).then(response =>{
    form.value = response.data;
    addOrUpdateDrawerVisible.value = true;
    title.value = "修改设备申请信息";
  })
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["equipmentApplicationRef"].validate(valid => {
    if (!valid) {
      if (form.value.id != undefined) {
        updateEquipmentApplicationInfo(form.value).then(response => {
          proxy.$message.success("修改成功")
          addOrUpdateDrawerVisible.value = false;
          getList();
        });
      } else {
        addEquipmentApplicationInfo(form.value).then(response => {
          proxy.$message.success("新增成功");
          addOrUpdateDrawerVisible.value = false;
          getList();
        });
      }
    }
  });

}
/** 删除按钮操作 */
function handleDelete(row) {
  const equipmentApplicationIds = row.id || ids.value
  proxy.$confirm('是否确认删除编号为"' + equipmentApplicationIds + '"的数据项？').then(function() {
    return delEquipmentApplicationInfo(equipmentApplicationIds);
  }).then(() => {
    getList();
    proxy.$message.success("删除成功");
    // 清空行选择器
    selectedKeys.value = []
    single.value = true;
    multiple.value = true;
  }).catch(() => {});
}

getList();
</script>
